﻿using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using System;
using System.Web;
using Framework.Caching;
using Framework;
using Entities;
using Entities.System;
using CommonLib;
using DataAccess.SQL;

namespace BLL.System
{
    public class LoginLogBO : BusinessBase
    {
        public static string get(Dictionary<string, string> dicLike,
            Dictionary<string, string> dicEqual, Dictionary<string, string> dicIn,
            string conditionAttach, int? pageSize, int? pageNum)
        {
            List<LoginCount> lst = new List<LoginCount>();
            var condition = getCondition(dicLike, dicEqual, dicIn) + conditionAttach;
            string strSql = string.Empty;

            if (pageSize.HasValue && pageNum.HasValue)
            {
                int from = pageSize.Value * (pageNum.Value - 1);
                int to = pageSize.Value * pageNum.Value;
                string strTableName = @"(SELECT  r.FUNCTION_KEY,r.FUNCTION_NAME,COUNT(*) AS LOGIN_COUNT FROM (
    SELECT * FROM SBRM_LOGIN_LOG l  WHERE 1=1 {0} 
    ) r 
GROUP BY r.FUNCTION_KEY, r.FUNCTION_NAME                                             
) u";
                strTableName = string.Format(strTableName, condition);
                strSql = Common.EasyUIHelper.GetSqlOfPageData(pageNum, pageSize, strTableName, "FUNCTION_KEY", "", "");
//                sql = string.Format(@"SELECT *
//              FROM (SELECT tt.*, ROWNUM AS rowno
//                      FROM (  SELECT  r.FUNCTION_KEY,r.FUNCTION_NAME,COUNT(*) AS LOGIN_COUNT
//                                FROM  (SELECT * FROM SBRM_LOGIN_LOG l  WHERE 1=1 {2} ) r GROUP BY  r.FUNCTION_KEY,r.FUNCTION_NAME                                             
//                              ORDER BY r.FUNCTION_NAME ) tt
//                     WHERE ROWNUM <= {0}) table_alias
//             WHERE table_alias.rowno >{1} ", to, from, condition);
            }
            else
            {

                strSql = string.Format(@"   SELECT  r.FUNCTION_KEY,r.FUNCTION_NAME,COUNT(*) AS LOGIN_COUNT
                                FROM  (SELECT * FROM SBRM_LOGIN_LOG l  WHERE 1=1 {0} ) r  GROUP BY  r.FUNCTION_KEY,r.FUNCTION_NAME                                                   
                              ORDER BY r.FUNCTION_NAME  ", condition);
            }

            try
            {
                lst = SqlHelper.ExecuteList<LoginCount>(strSql, CommandType.Text);
                //总条数
                strSql = string.Format("  select count(distinct(l.FUNCTION_KEY)) from  SBRM_LOGIN_LOG l  where  1=1 {0}", condition);
                object oCount = SqlHelper.ExecuteScalar(strSql, CommandType.Text);
                int total = Convert.ToInt32(oCount);
                return (new Result<LoginCount> { rows = lst, total = total }).ToJson();
            }
            catch (Exception ex)
            {
                var error = new Status { Success = false, Message = ex.Message };
                return error.ToJson();
            }
        }

        public static string getDetails(Dictionary<string, string> dicLike,
            Dictionary<string, string> dicEqual, string conditionAttach,
            int? pageSize, int? pageNum)
        {

            List<LoginLog> lst = new List<LoginLog>();
            var condition = getCondition(dicLike, dicEqual) + conditionAttach;

            string strSql = string.Empty;
            if (pageSize.HasValue && pageNum.HasValue)
            {
                int from = pageSize.Value * (pageNum.Value - 1);
                int to = pageSize.Value * pageNum.Value;
                string strTableName = @"(SELECT * FROM  SBRM_LOGIN_LOG l  where  1=1 {0} ) t";
                strTableName = string.Format(strTableName, condition);
                strSql = Common.EasyUIHelper.GetSqlOfPageData(pageNum, pageSize, strTableName, "FUNCTION_KEY", "", "");
//                strSql = string.Format(@"SELECT *
//              FROM (SELECT tt.*, ROWNUM AS rowno
//                      FROM (  SELECT *
//                                FROM  SBRM_LOGIN_LOG l  where  1=1 {2}                                                   
//                              ORDER BY l.LOGIN_DATE DESC ) tt
//                     WHERE ROWNUM <= {0}) table_alias
//             WHERE table_alias.rowno >{1} ", to, from, condition);
            }
            else
            {

                strSql = string.Format(@" SELECT *
                                FROM  SBRM_LOGIN_LOG l  where  1=1 {0}                                                   
                              ORDER BY l.LOGIN_DATE DESC  ", condition);
            }
            try
            {
                lst = SqlHelper.ExecuteList<LoginLog>(strSql, CommandType.Text);
                //总条数
                strSql = string.Format("  select count(*) from  SBRM_LOGIN_LOG l  where  1=1 {0}", condition);
                object oCount = SqlHelper.ExecuteScalar(strSql, CommandType.Text);
                int total = Convert.ToInt32(oCount);
                return (new Result<LoginLog> { rows = lst, total = total }).ToJson();

            }
            catch (Exception ex)
            {
                var error = new Status { Success = false, Message = ex.Message };
                return error.ToJson();

            }

        }

        public static string delLoginLog(HttpContext context)
        {

            string sql = string.Empty;
            object result;
            //删除
            List<string> sqlArr = new List<string>();
            sqlArr.Add(string.Format("delete from SBRM_LOGIN_LOG Where FUNCTION_KEY = '{0}'", context.Request.Form["FUNCTION_KEY"]));
            result = SqlHelper.ExecuteScalar(sqlArr[0], CommandType.Text);
            if (Convert.ToInt32(result) > 0) return (new Status { Success = true }).ToJson();
            else return (new Status { Success = false, Message = "执行异常!" }).ToJson();
        }
    }
}
